package tree_util;

import com.opensymphony.xwork2.ActionSupport;
import db_tool.DBConnection;
import db_tool.LogTool;
import db_tool.TimeFormat;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * 添加、更新、删除	鱼样本
 * */
public class Op_Fishes extends ActionSupport {

	private String flag;		//区分操作
	
	private final static String START = "FSS";				//主键、照片 开头
	
	private static String PATH = "";					//照片在服务器上的路径("|"隔开)
	
	private String SampleID;
	private String BodyLength;
	private String Length;
	private String BodyWeight;
	private String Age;
	private String ID_Catches;			//外键
	
	private List<File> Photo;					//文件引用
	private List<String> PhotoFileName;			//文件名字
	private List<String> PhotoContentType;		//文件类型
	private String isNull;

	private String deletePhoto;
	private String NodeName;
	
	private PrintWriter writer = null;
	
	private DBConnection db_connection = null;
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		writer = ServletActionContext.getResponse().getWriter();
		db_connection = DBConnection.getInstance();
		if(BodyLength != null && BodyLength.equals("")) {BodyLength="0";}
		if(Length != null && Length.equals("")) {Length="0";}
		if(BodyWeight != null && BodyWeight.equals("")) {BodyWeight="0";}
		if(Age != null && Age.equals("")) {Age="0";}
		if(flag.equals("insert")){
			//插入操作
			insert();
			LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "Fishes", 1);

		}else if(flag.equals("update")){
			//更新操作
			update();
            LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "Fishes", 1);

        }if(flag.equals("delete")){
			   System.out.println("delete......................................"+NodeName+"     "+deletePhoto);
			   delete();
            LogTool.insertIntoDataLog(LogTool.DELETE_CODE, "Fishes", 1);
        }else if(flag.equals("getAllPhoto")){
			PATH = UpLoadPicture.getAllPhoto(NodeName);
			System.out.println("......................................"+NodeName+"             "+PATH);
			writer.write(PATH);
		}
		return null;
	}
	
	private void delete(){
	    PATH = UpLoadPicture.Delete(NodeName,deletePhoto);
	    String update = "update Fishes set Photo='"
				+ PATH + "' where SampleID='" + NodeName + "'";
		System.out.println(update);
		Statement statement = null;
		try {
			statement = db_connection.getStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			statement.executeUpdate(update);
			writer.write(PATH);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			writer.write("error");
			System.out.println("--网具更新失败--");
			e.printStackTrace();
		} finally{
			try {
				db_connection.close(statement);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("网具更新操作关闭失败");
				e.printStackTrace();
			}
		}
		
	}
	
	private void update(){
		//获取照片路径
		if(isNull.length()!=0)
		PATH = UpLoadPicture.upload(Photo, PhotoFileName, SampleID);
		if(PATH.equals("photo_error")){			//照片出错
			writer.write("photo_error");
		}else{
			String update="";
			if(isNull.length()!=0){
			 update = "update Fishes set Photo='" + PATH + "',BodyLength='"
					+ BodyLength + "',Length='" + Length + "',BodyWeight='" + BodyWeight
					+ "',Age='" + Age + "' where SampleID='" + SampleID + "'";
			}else{
				 update = "update Fishes set BodyLength='"
							+ BodyLength + "',Length='" + Length + "',BodyWeight='" + BodyWeight
							+ "',Age='" + Age + "' where SampleID='" + SampleID + "'";
			}
			
			Statement statement = null;
			try {
				statement = db_connection.getStatement();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				statement.executeUpdate(update);
				writer.write(PATH);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				writer.write("error");
				System.out.println("--鱼样本更新失败--");
				e.printStackTrace();
			} finally{
				try {
					db_connection.close(statement);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					System.out.println("鱼样本更新操作关闭失败");
					e.printStackTrace();
				}
			}
		}
	}
	
	private void insert(){
		String childID = START + TimeFormat.getNowTime();
		//获取照片路径
		if(isNull.length()!=0)
		PATH = UpLoadPicture.upload(Photo, PhotoFileName, childID);
		if(PATH.equals("photo_error")){			//照片出错
			writer.write("photo_error");
		}else{
			String insert = "insert into Fishes values(?,?,?,?,?,?,?)";
			
			PreparedStatement preparedStatement = null;
			try {
				preparedStatement = db_connection.getPreparedStatement(insert);
				preparedStatement.setString(1, childID);
				preparedStatement.setString(2, PATH);
				preparedStatement.setString(3, BodyLength);
				preparedStatement.setString(4, Length);
				preparedStatement.setString(5, BodyWeight);
				preparedStatement.setString(6, Age);
				preparedStatement.setString(7, ID_Catches);
				
				preparedStatement.executeUpdate();
				writer.write(childID);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				writer.write("error");
				System.out.println("--鱼样本插入错误--");
				e.printStackTrace();
			}finally{
				try {
					db_connection.close(preparedStatement);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					System.out.println("鱼样本插入操作关闭失败");
					e.printStackTrace();
				}
			}
		}
	}

	public List<String> getPhotoFileName() {
		return PhotoFileName;
	}

	public void setPhotoFileName(List<String> photoFileName) {
		PhotoFileName = photoFileName;
	}

	public List<String> getPhotoContentType() {
		return PhotoContentType;
	}

	public void setPhotoContentType(List<String> photoContentType) {
		PhotoContentType = photoContentType;
	}
	
	public void setPhoto(List<File> photo) {
		Photo = photo;
	}

	public List<File> getPhoto() {
		return Photo;
	}

	public String getBodyLength() {
		return BodyLength;
	}
	public void setBodyLength(String bodyLength) {
		BodyLength = bodyLength;
	}
	public String getLength() {
		return Length;
	}
	public void setLength(String length) {
		Length = length;
	}
	public String getBodyWeight() {
		return BodyWeight;
	}
	public void setBodyWeight(String bodyWeight) {
		BodyWeight = bodyWeight;
	}
	public String getAge() {
		return Age;
	}
	public void setAge(String age) {
		Age = age;
	}
	public String getID_Catches() {
		return ID_Catches;
	}
	public void setID_Catches(String iD_Catches) {
		ID_Catches = iD_Catches;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getSampleID() {
		return SampleID;
	}

	public void setSampleID(String sampleID) {
		SampleID = sampleID;
	}
	
	public String getDeletePhoto(){
		return deletePhoto;
	}
	
	public void setDeletePhoto(String deletePhoto){
		this.deletePhoto=deletePhoto;
	}
	
	public String getNodeName(){
		return NodeName;
	}
	
	public void setNodeName(String NodeName){
		this.NodeName=NodeName;
	}
	
	
	public String getIsNull(){
		return isNull;
	}
	
	public void setIsNull(String isNull){
		this.isNull=isNull;
	}
}
